home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3976 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.uni-stuttgart.de!schweikh
  2. From: schweikh@itosun.ito.uni-stuttgart.de (Jens Schweikhardt)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: cpp question
  5. Date: 1 Feb 1996 07:33:15 GMT
  6. Organization: Comp.Center (RUS), U of Stuttgart, FRG
  7. Message-ID: <4epqbr$206q@info4.rus.uni-stuttgart.de>
  8. References: <4drm99$j0m@peabody.colorado.edu> <4eloq7$h9u@bcfreenet.seflin.lib.fl.us>
  9. NNTP-Posting-Host: itosun.ito.uni-stuttgart.de
  10.  
  11. In article <4eloq7$h9u@bcfreenet.seflin.lib.fl.us>,
  12. Ralph Silverman <z007400b@bcfreenet.seflin.lib.fl.us> wrote:
  13. :WOOD  JAMEY RYAN (woodjr@rintintin.Colorado.EDU) wrote:
  14. :: I have something like this:
  15. :
  16. ::      #define USERLEN 8
  17. ::      #define HOSTLEN 15
  18. :
  19. ::      char name[] = "woodjr";
  20. ::      char host[] = "really.long.hostname.com";
  21. ::      char s[100];
  22. :
  23. ::      sprintf(s, "%.USERLENs@%.HOSTLENs", name, host);
  24. :
  25. :: And I want cpp to parse the sprintf line to:
  26. :
  27. ::      sprintf(s, "%.8s@%.15s", name, host);
  28. :
  29. :: Which it (of course) isn't doing.  Is there some syntax I
  30. :: can use to get it to do this?
  31. :
  32. :: Thankyou,
  33. :: Jamey Wood
  34. :
  35. :--
  36. :***********begin r.s. response**********
  37. :
  38. :    cpp will not expand macro into
  39. :    string bounded by
  40. :        ""
  41. :    .
  42. :
  43. :***********end r.s. response************
  44. #### begin r.s. criticism #### YUCK!
  45.  
  46. Not too helpful an answer I think.
  47. There are solutions to the original poster's problem.
  48. One of them is, if you know that USERLEN is 8 everywhere,
  49. just use a
  50.  
  51. #define USERLEN "8"
  52. #define HOSTLEN "15"
  53.  
  54. and use the concatenation of string literals:
  55.  
  56. sprintf(s, "%." USERLEN "s@%." HOSTLEN "s", name, host);
  57.  
  58. Another way, where USERLEN may even be a runtime variable is to
  59. use * as the precision specifier and pass another arg to sprintf.
  60. E.g.
  61.  
  62.     printf ("%*s", num_spaces, "");
  63.  
  64. will print num_spaces spaces.  Technically, * is used to specify _width_ here,
  65. not _precision_. But you may also use
  66. printf ("%*.*s", width, precision, string);
  67. You get the idea.
  68.  
  69. For more information contact your friendly printf(3) manual page :-)
  70.  
  71. cc: to original poster
  72.  
  73. #### end r.s. criticism #### YUCK!
  74.  
  75. -- 
  76. SIGSIG -- signature too long (core dumped)
  77.